package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpModel;
import com.monitor.entity.ErpSpecifications;
import com.monitor.service.ErpModelService;
import com.monitor.service.ErpSpecificationsService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品名规格 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-20
 */
@RestController
@RequestMapping("/erpSpecifications")
@Api(description=" 品名规格")
public class ErpSpecificationsController {

    @Resource
    private ErpSpecificationsService specificationsService;

    @Resource
    private ErpModelService erpModelService;

    /*@RequestMapping("/getSpecToTree")
    @ApiOperation("查询品名规格信息得到树状结构")
    @ApiImplicitParams({
           *//* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*//*
    })
    public @ResponseBody
    Result<List<ErpSpecifications>> getSpecToTree() {
        Result<List<ErpSpecifications>> result=new Result<List<ErpSpecifications>>();
        try{
            List<ErpSpecifications> Specs=specificationsService.selectList(new EntityWrapper<ErpSpecifications>().eq("iszh",0));
            for (ErpSpecifications Spec:Specs) {
                specificationsService.getSpecifications(Spec);
            }
            result.setCount(Specs.size());
            result.setData(Specs);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

*/

    @RequestMapping("/inertOrUpdateSpecCustomer")
    @ApiOperation("添加品名规格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateSpecCustomer(ErpSpecifications Spec) {


        Result<Integer> result=new Result<Integer>();
        ErpSpecifications erpSpecifications1=new ErpSpecifications();

        try{
            erpSpecifications1.setZid(Spec.getPid());
        if (Spec.getAddOrXiuGai()==false) {
            ErpSpecifications erpSpecifications = specificationsService.selectById(Spec.getId());
            erpSpecifications.setZid(erpSpecifications.getZid());
                specificationsService.updateById(Spec);

        }

         else   if (Spec.getPid().equals("SF0")&&Spec.getAddOrXiuGai()==true){
                Spec.setIszh(0);
                ErpModel erpSpecifications=new ErpModel();
                erpSpecifications.setIszh(0);
                erpSpecifications.setPid("MO0");
                erpSpecifications.setName(Spec.getName());
                erpSpecifications.setZid(Spec.getZid());
                erpModelService.insert(erpSpecifications);
            result.setData(specificationsService.insertOrUpdate(Spec)?1:0);
            }else if ( specificationsService.selectOne(new EntityWrapper<ErpSpecifications>().eq("zid",Spec.getPid())).getPid().equals("SF0")&&Spec.getAddOrXiuGai()==true){

////            char[] pid = Spec.getPid().toCharArray();
////            char[] zid = Spec.getZid().toCharArray();
////            pid[0] ='M';
////            pid[1] ='O';
////            pid[2] ='0';
//            StringBuilder strBuilder = new StringBuilder(Spec.getPid());
//            strBuilder.setCharAt(0, 'X');
//            str=Builder.toString();
            Spec.setIszh(1);
            ErpModel erpSpecifications=new ErpModel();
            erpSpecifications.setIszh(1);
            erpSpecifications.setPid(Spec.getPid());
            erpSpecifications.setName(Spec.getName());
            erpSpecifications.setZid(Spec.getZid());
            erpModelService.insert(erpSpecifications);
            result.setData(specificationsService.insertOrUpdate(Spec)?1:0);
            }else {
                Spec.setIszh(2);
            result.setData(specificationsService.insertOrUpdate(Spec)?1:0);
            }
            result.setCount(1);

        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/UpdateSpecCustomer")
    @ApiOperation("修改品名规格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> UpdateSpecCustomer(ErpSpecifications Spec) {
        Result<Integer> result=new Result<Integer>();

        try {
            result.setData( specificationsService.updateById(Spec)?1:0);
            result.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
        }
            return result;
    }

    @RequestMapping("/getSpecCustomerById")
    @ApiOperation("根据品名规格id品名规格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "SpecId", value = "品名规格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpSpecifications> getSpecCustomerById(Integer SpecId) {
        Result<ErpSpecifications> result=new Result<ErpSpecifications>();
        try{
            result.setCount(1);
            result.setData(specificationsService.selectById(SpecId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeSpecCustomerById")
    @ApiOperation("根据品名规格id删除品名规格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "SpecId", value = "品名规格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeSpecCustomerById(Integer SpecId) {
        Result<Integer> result=new Result<Integer>();
        try{
            ErpSpecifications erpSpecifications = specificationsService.selectById(SpecId);
            result.setData(specificationsService.deleteById(SpecId)?1:0);
      if (true){
          List<ErpSpecifications> pid = specificationsService.selectList(new EntityWrapper<ErpSpecifications>().eq("pid", erpSpecifications.getZid()));
          List<String>   ids=new ArrayList<>();
          if (pid.size()!=0)
          pid.parallelStream().forEach(p->{
              ids.add(p.getId()+"");
          });
          if (ids.size()!=0)
          specificationsService.deleteBatchIds(ids);
          pid.parallelStream().forEach(spec->{
              if (spec!=null){
                  List<ErpSpecifications> pid1 = specificationsService.selectList(new EntityWrapper<ErpSpecifications>().eq("pid", spec.getZid()));
                  List<String>   idss=new ArrayList<>();
                  if (pid1.size()!=0)
                  pid1.parallelStream().forEach(p->{
                      idss.add(p.getId()+"");
                  });
                  if (idss.size()!=0)
                  specificationsService.deleteBatchIds(idss);
                  pid1.parallelStream().forEach(specc->{
                      List<ErpSpecifications> pid2 = specificationsService.selectList(new EntityWrapper<ErpSpecifications>().eq("pid", specc.getZid()));
                      List<String>   idsss=new ArrayList<>();
                      if (pid2.size()!=0)
                      pid2.parallelStream().forEach(p->{
                          idsss.add(p.getId()+"");
                      });
                      specificationsService.deleteBatchIds(idsss);
                  });
              }
          });
      }

            result.setCount(1);

        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getSpecCustomerBySpecIdPage")
    @ApiOperation("根据品名规格ID查询品名规格分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "SpecId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Page<ErpSpecifications>> getSpecCustomerBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                        @RequestParam(value="limit",defaultValue="10") int limit) {
        Result<Page<ErpSpecifications>> result=new Result<Page<ErpSpecifications>>();
        try{
            Page<ErpSpecifications> page1=new Page<ErpSpecifications>(page,limit);
            Page<ErpSpecifications> carPage=specificationsService.selectPage(page1,new EntityWrapper<ErpSpecifications>());
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getSpecCustomerBySpecIdList")
    @ApiOperation("查询所有品名规格")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "SpecId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpSpecifications>> getSpecCustomerBycustIdList() {
        Result<List<ErpSpecifications>> result=new Result<List<ErpSpecifications>>();
        try{
            List<ErpSpecifications> carPage=specificationsService.getSpecCustomerBycustIdList();
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

